package demo.log4j2;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Log4j2Demo {

    private static final Logger logger = LogManager.getLogger(Log4j2Demo.class);


    public static void main(String[] args) throws InterruptedException {
        System.out.println("------------- start! -------------");
        for (int i = 0; i < 10; i++) {
            String msg = "Hello " + i;
            logger.warn(msg);
            Slf4jTest.log(msg);
            Thread.sleep(5000);
        }
        System.out.println("------------- end -------------");
    }

    public void init() {
        logger.info("init");
        File file = new File("log4j2.xml");
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(file));
            final ConfigurationSource source = new ConfigurationSource(in);
            Configurator.initialize(null, source);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
